class Solution {

    public List<List<Integer>> towSum(int[] r, int t) {
        List<List<Integer>> ret = new ArrayList<>();
        Set<Integer> set = new HashSet<>();
        for(int i = 0; i < r.length; i++) {
            int ft = t - r[i];
            if (set.contains(ft)) {
                ret.add(Arrays.asList(r[i], ft));
                //continue;
            }
            set.add(ft);
        }
        return ret;
    }

    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> ret = new ArrayList<>();
        for(int i = 0; i < nums.length; i++) {
            int loop = nums[i];
            List<List<Integer>> lr = towSum(Arrays.copyOfRange(nums, i, nums.length - 1), 0 - loop);
            if (!lr.isEmpty()) {
                for(List<Integer> item : lr) {
                    List<Integer> newItem = new ArrayList<>();
                    newItem.add(loop);
                    newItem.addAll(item);
                    ret.add(newItem);
                }
            }
        }

        return ret;
    }
}
